package com.tan.lbs.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;

import com.tan.lbs.dao.ItemDAO;
import com.tan.lbs.entity.Designer;
import com.tan.lbs.entity.Game;
import com.tan.lbs.entity.Item;
import com.tan.lbs.entity.Location;
import com.tan.lbs.util.HibernateUtil;

public class ItemDAOImpl implements ItemDAO {

	@Override
	public String create(Item item) {
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session s = sf.getCurrentSession();
		s.beginTransaction();
		s.save(item);
		s.getTransaction().commit();
		return item.getItemID();

	}

	@Override
	public Item readByID(String itemID) {
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session s = sf.getCurrentSession();
		s.beginTransaction();
		Item item = (Item)s.get(Item.class, itemID);
		s.getTransaction().commit();
		return item;
	}

	@Override
	public void update(Item item) {
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session s = sf.getCurrentSession();
		s.beginTransaction();
		s.update(item);
		s.getTransaction().commit();
		
	}

	@Override
	public ArrayList<Item> searchByGame(String gameID) {
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session s = sf.getCurrentSession();
		s.beginTransaction();
		List<Item> gs = s.createCriteria(Item.class).add(Restrictions.
				eq("gameID", gameID)).list();
		s.getTransaction().commit();
		return new ArrayList<Item>(gs);
	}

	@Override
	public void delete(String itemID) {
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session s = sf.getCurrentSession();
		s.beginTransaction();
		Item item = new Item();
		item.setItemID(itemID);
		s.delete(item);
		s.getTransaction().commit();	
	}

}
